#!/usr/bin/python
#
# Tom's Pong
# A simple pong game with realistic physics and AI
# http://www.tomchance.uklinux.net/projects/pong.shtml
#
# Released under the GNU General Public License

VERSION = "0.4"

try:
    import sys
    import random
    import math
    import os
    import getopt
    import pygame
    from socket import *
    from pygame.locals import *
except ImportError, err:
    print "couldn't load module. %s" % (err)
    sys.exit(2)

SCREEN_SIZE = (240,320)
BLOCK_SIZE = SCREEN_SIZE[0] / 4
Screen_rect = pygame.Rect((0,0),SCREEN_SIZE)

def pos_to_block(pos):
    return (pos[0]/BLOCK_SIZE, pos[1]/BLOCK_SIZE)
    
class Block(pygame.sprite.Sprite):
    """A ball that will move across the screen
    Returns: ball object
    Functions: update, calcnewpos
    Attributes: area, vector"""

    def __init__(self, color, pos, size):
        pygame.sprite.Sprite.__init__(self)
        self.size = (size[0] * BLOCK_SIZE , size[1] * BLOCK_SIZE)
        self.color = color
        self.image = pygame.Surface(self.size)
        self.image.fill(color)
        pygame.draw.rect( self.image, (255,255,255), self.image.get_rect(),2 )
        #pygame.draw.line( self.image, (255,255,255), (0,0),(self.size[0]-2,0),2);
        #pygame.draw.line( self.image, (255,255,255), (self.size[0]-2,0),(self.size[0]-2,self.size[1]-2),2);

        self.rect = self.image.get_rect()
        self.rect.topleft = (pos[0] * BLOCK_SIZE , pos[1] * BLOCK_SIZE)
        
    def update(self):
        pass
       
    def try_move(self, sprites, pos_initial, pos_current):
        old_block_pos = pos_to_block(pos_initial)
        new_block_pos = pos_to_block(pos_current)
        if old_block_pos == new_block_pos:
            self.rect.topleft = self.saved_topleft
            return
        self.rect.topleft = ( self.saved_topleft[0] + ( new_block_pos[0] - old_block_pos[0] ) * BLOCK_SIZE,
                                self.saved_topleft[1] + ( new_block_pos[1] - old_block_pos[1] ) * BLOCK_SIZE )
        
        if not Screen_rect.contains( self.rect):
            self.rect.topleft = self.saved_topleft
            return
        
        for s in sprites:
            if s == self: continue
            if self.rect.colliderect( s.rect):
                self.rect.topleft = self.saved_topleft
                return
                
        print old_block_pos, new_block_pos
        print self.saved_topleft, self.rect
                
        
    def capture(self):
        self.saved_topleft = self.rect.topleft
        print 'captured'

def hit_test(blocks, pos):
    for b in blocks.sprites():
        if b.rect.collidepoint(pos):
            return b
    
def main():
    # Initialise screen
    pygame.init()
    screen = pygame.display.set_mode(SCREEN_SIZE)
    pygame.display.set_caption('HuaRongDao')

    # Fill background
    background = pygame.Surface(screen.get_size())
    background = background.convert()
    background.fill((0, 0, 0))

    players = [ Block([255,0,0],[0,0],[1,2]),
                Block([255,255,0],[1,0],[2,2]),
                Block([255,0,0],[3,0],[1,2]),
                Block([0,255,0],[0,2],[1,2]),
                Block([0,255,0],[3,2],[1,2]),
                Block([0,255,255],[1,2],[2,1]),
                Block([0,0,255],[0,4],[1,1]),
                Block([0,0,255],[3,4],[1,1]),
                Block([0,0,255],[1,3],[1,1]),
                Block([0,0,255],[2,3],[1,1]),
                ]
    
    # Initialise sprites
    blocksprites = pygame.sprite.RenderPlain(players)

    # Blit everything to the screen
    screen.blit(background, (0, 0))
    pygame.display.flip()

    # Initialise clock
    clock = pygame.time.Clock()
    capture_block = None
    capture_pos = (0,0)
    # Event loop
    while 1:
        # Make sure game doesn't run at more than 60 frames per second
        clock.tick(60)

        for event in pygame.event.get():
            if event.type == QUIT or event.type == KEYDOWN:
                return
            if event.type == MOUSEBUTTONDOWN:
                block = hit_test( blocksprites, event.pos )
                if block:
                    capture_block = block
                    capture_pos = event.pos
                    capture_block.capture()
                    
            if event.type == MOUSEMOTION:
                if capture_block:
                    capture_block.try_move( blocksprites, capture_pos, event.pos )
                    
            if event.type == MOUSEBUTTONUP:
                if capture_block:
                    capture_block.try_move( blocksprites, capture_pos, event.pos )
                    capture_block = None
                    # count for move
        screen.blit(background, Screen_rect, Screen_rect)
        #screen.blit(background, player1.rect, player1.rect)
        #screen.blit(background, player2.rect, player2.rect)
        blocksprites.update()
        blocksprites.draw(screen)
        pygame.display.flip()


if __name__ == '__main__': main()
